package com.jy.modules.cims.welcome.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.component.common.service.TCMmCooperorgDealService;
import com.jy.modules.cims.data.common.dto.TCMmCooperorgDealDTO;
import com.jy.modules.cims.data.dto.DealDto;
import com.jy.modules.cims.data.dto.MyTaskDto;
import com.jy.modules.cims.deal.service.DealService;
import com.jy.platform.core.message.DataMsg;
import com.jy.platform.core.message.PageParameter;
import com.jy.platform.core.message.PageUtil;
import com.jy.platform.core.message.QueryReqBean;
import com.jy.platform.restservice.web.base.BaseController;


 
@Controller
@Scope("prototype")
@RequestMapping("/welcomeDeal")
public class WelcomeController  extends BaseController{
	@Autowired
	@Qualifier("com.jy.modules.cims.deal.service.DealService")
	private DealService dealService;
	
	@RequestMapping(value = "/prepareExecute/{operate}")
    public ModelAndView execute(@PathVariable("operate") String operate) throws Exception {
        ModelAndView model = new ModelAndView();
        if("toQueryPage".equals(operate)){//跳转至 查询具体交易类型页面
        	
        	model.setViewName("cims/welcome/myTaskList");
        	model.addObject("dealType", this.getParameterString("dealType"));//  交易类型
        }else{
        	if("toQueryPageaAll".equals(operate)){//跳转至 查询全部页面
            	model.setViewName("cims/welcome/myTaskListAll");
            }  
        } 
        return model;
     }
	
	/**
	 * 查询全部（债权转让、债权转出、二次购买、贷款转出）待办事项
	 * @param dto
	 * @param dataMsg
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/searchMyTaskListByPaging")
	@ResponseBody
	public DataMsg searchMyTaskListByPaging(TCMmCooperorgDealDTO dto,@ModelAttribute DataMsg dataMsg) throws Exception {
		logger.info("queryMyTaskListByPaging:--------------------------begin");
		Map<String, Object> searchParams = new HashMap<String, Object>();
    	searchParams.put("dto", dto);
        QueryReqBean params = new QueryReqBean();
        params.setSearchParams(searchParams);
    	PageParameter pageInfo = PageUtil.toPageParameter(dataMsg);
		params.setPageParameter(pageInfo);
		
		List<DealDto> dataList = dealService.searchMyTaskListByPaging(params.getSearchParams());
		dataMsg.setData(dataList);
		dataMsg.setTotalRows(pageInfo.getTotalCount());
		
		logger.info("queryMyTaskListByPaging:--------------------------end");
		return dataMsg;
	}
	
	/**
	 * 查询债权转让、债权转出、二次购买待办事项
	 * @param request
	 * @param dataMsg
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryMyTaskListDetail")
	@ResponseBody
	public  DataMsg  searchMyTaskListDetail(HttpServletRequest request,TCMmCooperorgDealDTO dto,@ModelAttribute DataMsg dataMsg) throws Exception {
		logger.info("searchMyTaskListDetail:--------------------------begin");
		Map<String, Object> searchParams = new HashMap<String, Object>();
    	searchParams.put("dto", dto);
    	
        QueryReqBean params = new QueryReqBean();
        params.setSearchParams(searchParams);
    	PageParameter pageInfo = PageUtil.toPageParameter(dataMsg);
		params.setPageParameter(pageInfo);
		
		List<DealDto> dataList = dealService.searchMyTaskListDetailByPaging(params.getSearchParams());
		dataMsg.setData(dataList);
		dataMsg.setTotalRows(pageInfo.getTotalCount());
		
		logger.info("queryMyTaskListDetail:--------------------------end");
		return dataMsg;
	}
	/**
	 * 查询贷款转出 待办事项
	 * @param request
	 * @param dataMsg
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryMyTaskListLoanDetail")
	@ResponseBody
	public  DataMsg  searchMyTaskListLoanDetail(HttpServletRequest request,TCMmCooperorgDealDTO dto,@ModelAttribute DataMsg dataMsg) throws Exception {
		logger.info("searchMyTaskListLoanDetail:--------------------------begin");
		Map<String, Object> searchParams = new HashMap<String, Object>();
    	searchParams.put("dto", dto);
    	
        QueryReqBean params = new QueryReqBean();
        params.setSearchParams(searchParams);
    	PageParameter pageInfo = PageUtil.toPageParameter(dataMsg);
		params.setPageParameter(pageInfo);
		
		List<DealDto> dataList = dealService.searchMyTaskListLoanDetailByPaging(params.getSearchParams());
		dataMsg.setData(dataList);
		dataMsg.setTotalRows(pageInfo.getTotalCount());
		
		logger.info("searchMyTaskListLoanDetail:--------------------------end");
		return dataMsg;
	}
	
	/**
	 * 统计债权转出、债权受让、二次购买、贷款转出待办事项总记录数
	 * @param request
	 * @param dataMsg
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryMyTaskList")
	@ResponseBody
	public  DataMsg  queryMyTaskList(HttpServletRequest request,@ModelAttribute DataMsg dataMsg) throws Exception {
		List<MyTaskDto> myTaskList = new ArrayList<MyTaskDto>();
		Map<String, Object> searchParams = new HashMap<String, Object>();
		TCMmCooperorgDealDTO dto = null;
		MyTaskDto myTaskDto =null;
		
		
		dto = new TCMmCooperorgDealDTO();
		PageParameter pageInfo = PageUtil.toPageParameter(dataMsg);
		QueryReqBean params = new QueryReqBean();
		params.setPageParameter(pageInfo);
		
		// 交易类型为：债权转让的待办记录
		dto.setDealType(CimsConstant.DEAL_TYPE_CREDIT_OUT); // 债权转让交易
		searchParams.put("dto", dto);
        params.setSearchParams(searchParams);
        dealService.searchMyTaskListDetailByPaging(params.getSearchParams());
		int creditOutAccount = pageInfo.getTotalCount();
		logger.info("creditOutAccount:--------------------------"+creditOutAccount);
		
		// 交易类型为：债权受让的待办记录
		dto = new TCMmCooperorgDealDTO();
		dto.setDealType(CimsConstant.DEAL_TYPE_CREDIT_IN); // 债权受让交易
		searchParams.put("dto", dto);
		params.setSearchParams(searchParams);
		dealService.searchMyTaskListDetailByPaging(params.getSearchParams());
		int creditInAccount = pageInfo.getTotalCount();
		logger.info("creditInAccount:--------------------------"+creditInAccount);
		
		// 交易类型为：二次购买的待办记录
		dto = new TCMmCooperorgDealDTO();
		dto.setDealType(CimsConstant.DEAL_TYPE_SECOND_PURCHASE); // 二次购买
		searchParams.put("dto", dto);
		params.setSearchParams(searchParams);
		dealService.searchMyTaskListDetailByPaging(params.getSearchParams());
		int secondPurchaseAccount = pageInfo.getTotalCount();
		logger.info("secondPurchaseAccount:--------------------------"+secondPurchaseAccount);
		
		
		// 交易类型为：贷款转出 的待办记录
		dto = new TCMmCooperorgDealDTO();
		dto.setDealType(CimsConstant.DEAL_TYPE_LOAN_OUT); // 贷款转出
		searchParams.put("dto", dto);
		params.setSearchParams(searchParams);
		dealService.searchMyTaskListLoanDetailByPaging(params.getSearchParams());
		int loanOutAccount = pageInfo.getTotalCount();
		logger.info("loanOutAccount:--------------------------"+loanOutAccount);
		
		if(creditOutAccount != 0){
			myTaskDto = new MyTaskDto();
			myTaskDto.setDealType(CimsConstant.DEAL_TYPE_CREDIT_OUT); // 01-债权转让交易
			myTaskDto.setAccountNum(creditOutAccount+"");
			myTaskList.add(myTaskDto);
		}
		if(creditInAccount != 0){
			myTaskDto = new MyTaskDto();
			myTaskDto.setDealType(CimsConstant.DEAL_TYPE_CREDIT_IN); // 02-债权受让交易
			myTaskDto.setAccountNum(creditInAccount+"");
			myTaskList.add(myTaskDto);
		}
		if(secondPurchaseAccount != 0){
			myTaskDto = new MyTaskDto();
			myTaskDto.setDealType(CimsConstant.DEAL_TYPE_SECOND_PURCHASE); // 09-二次购买
			myTaskDto.setAccountNum(secondPurchaseAccount+"");
			myTaskList.add(myTaskDto);
		}
		if(loanOutAccount != 0){
			myTaskDto = new MyTaskDto();
			myTaskDto.setDealType(CimsConstant.DEAL_TYPE_LOAN_OUT); // 03-贷款转出交易
			myTaskDto.setAccountNum(loanOutAccount+"");
			myTaskList.add(myTaskDto);
		}
		
		dataMsg.setData(myTaskList);
		dataMsg.setTotalRows(myTaskList.size());
	 return dataMsg;
	}
}
